/*
 * Copyright (C) 2020 Agrui
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#include "IpcClientManager.h"
#include "IpcChannelManager.h"
#include "OSMemory.h"
#include "IpcClient.h"
#include "stdio.h"

static IpcClientManager* mInstance = nullptr;

static void onChannelDataReceive(void* callbackData, int id, void* data, int dataLen) {
    IpcClientManager* clientManager = (IpcClientManager*)callbackData;
    clientManager->handleChannelDataReceive(id, data, dataLen);
}

static void onChannelStateChanged(void* callbackData, int id, int state) {
    IpcClientManager* clientManager = (IpcClientManager*)callbackData;
    clientManager->handleChannelStateChanged(id, state);
}

IpcClientManager::IpcClientManager() {
    mList = newTransactionList();
    mTransactionAtomic = newAtomic(0);
    auto channelManager = getDefaultIpcChannelManager();
    registerIpcChannelStateChanged(channelManager, 0, onChannelStateChanged, this);
    registerIpcChannelStateChanged(channelManager, 1, onChannelStateChanged, this);

    registerIpcChannelDataReceiveInd(channelManager, 0, onChannelDataReceive, this);
    registerIpcChannelDataReceiveInd(channelManager, 1, onChannelDataReceive, this);

    //mInstance = this;
}

void IpcClientManager::handleChannelStateChanged(int id, int state) {


}
void IpcClientManager::handleChannelDataReceive(int id, void* data, int dataLen) {
    IpcDataHeadInfo* ipcHeader = (IpcDataHeadInfo*)data;
    if (ipcHeader->type == IPC_TYPE_RESPONSE) {
        TransactionNode* node = getTransactionNode(mList, ipcHeader->transaction);
        if (node && node->userData) {
            int len = ipcHeader->dataLen;
            int resultCode = ipcHeader->resultCode;
            int msgId = ipcHeader->msgId;
            int serviceId = ipcHeader->serviceId;
            ipcClientHandleRsp(serviceId, msgId, resultCode, ++ipcHeader, len, node->userData);
        }
        if (node) {
            OS_FREE(node);
        }
    } else if (ipcHeader->type == IPC_TYPE_INDICATION) {
        return;
    } else {
        return;
    }
}

IpcClientManager::~IpcClientManager() {
    if (mList) {
        deleteTransactionList(mList);
    }

    if (mTransactionAtomic) {
        deleteAtomic(mTransactionAtomic);
    }

    mInstance = NULL;
}

void IpcClientManager::addTransaction(int id, void* userData) {
    appendTransactionNode(mList, id, userData);
}

int IpcClientManager::nextTransaction() {
    return atomicInc(mTransactionAtomic);
}

